#!/bin/bash

#
# This script installs the required build-time dependencies
# and builds AppImages for AppImageKit
#

STRIP="strip"
STATIC_BUILD=1
JOBS=${JOBS:-1}
RUN_TESTS=0

while [ $1 ]; do
  case $1 in
    '--debug' | '-d' )
      STRIP="true"
      ;;
    '--use-shared-libs' | '-s' )
      STATIC_BUILD=0
      ;;
    '--run-tests' | '-t' )
      RUN_TESTS=1
      ;;
    '--clean' | '-c' )
      rm -rf build
      git clean -df
      rm -rf squashfuse/* squashfuse/.git
      rm -rf squashfs-tools/* squashfs-tools/.git
      exit
      ;;
    '--help' | '-h' )
      echo 'Usage: ./build.sh [OPTIONS]'
      echo
      echo 'OPTIONS:'
      echo '  -h, --help: Show this help screen'
      echo '  -d, --debug: Build with debug info.'
      echo '  -n, --no-dependencies: Do not try to install distro specific build dependencies.'
      echo '  -s, --use-shared-libs: Use distro provided shared versions of inotify-tools and openssl.'
      echo '  -c, --clean: Clean all artifacts generated by the build.'
      exit
      ;;
  esac

  shift
done


if cat /etc/*release | grep "CentOS" 2>&1 >/dev/null; then
    if [ -e /opt/rh/devtoolset-4/enable ]; then
        . /opt/rh/devtoolset-4/enable
    fi
fi

echo "$KEY" | md5sum

set -e
set -x

HERE="$(dirname "$(readlink -f "${0}")")"
cd "$HERE"

# Fetch git submodules
git submodule update --init --recursive

# Clean up from previous run
[ -d build/ ] && rm -rf build/

# Build AppImage
mkdir build
cd build

# make sure that deps in separate install tree are found
export PKG_CONFIG_PATH=/deps/lib/pkgconfig/

# make CMake use the right tools for ARM cross-compiling using toolchain file
if [ "$ARCH" == "armhf" ]; then
    export PATH=/deps/bin:"$PATH"
    export EXTRA_CMAKE_FLAGS="-DCMAKE_TOOLCHAIN_FILE=$HERE/cmake/toolchains/arm-linux-gnueabihf.cmake"
elif [ "$ARCH" == "aarch64" ]; then
    export PATH=/deps/bin:"$PATH"
    export EXTRA_CMAKE_FLAGS="-DCMAKE_TOOLCHAIN_FILE=$HERE/cmake/toolchains/aarch64-linux-gnu.cmake"
fi

cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON -DAPPIMAGEKIT_PACKAGE_DEBS=ON "${EXTRA_CMAKE_FLAGS[@]}"
make -j$JOBS
make install DESTDIR=install_prefix/

if [ -d /deps/lib ]; then
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":/deps/lib/
fi

xxd src/runtime | head -n 1

# Do NOT strip runtime
find install_prefix/usr/bin/ -not -iname runtime -print -exec "$STRIP" "{}" \; 2>/dev/null

ls -lh install_prefix/usr/bin/
for FILE in install_prefix/usr/bin/*; do
  echo "$FILE"
  ldd "$FILE" || true
done

bash -ex "$HERE/build-appdir.sh"

ls -lh

mkdir -p out
cp -r install_prefix/usr/{bin,lib/appimagekit}/* appdirs/*.AppDir out/
